//
//  ZCRaiseCell.m
//  zctx
//
//  Created by 郭亚京 on 15/10/9.
//  Copyright © 2015年 sanliang. All rights reserved.
//

#import "ZCRaiseCell.h"

@interface ZCRaiseCell() <UITextViewDelegate>

@end

@implementation ZCRaiseCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier cellType:(RaiseCellType)type {
    
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        if (type == RaiseCellTypeInput) {
            [self addLabelTitle];
            [self addTextField];
        } else if (type == RaiseCellTypeSelect) {
            [self addLabelTitle];
            [self addButtonProvince];
            [self addButtonCity];
        } else if (type == RaiseCellTypePush) {
            [self addLabelTitle];
            self.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            [self addLabelInfo];
        } else if (type == RaiseCellTypeText) {
            [self addTextView];
        }
    }
    
    return self;
}

- (void)addLabelTitle {
    [self.contentView addSubview:self.labelTitle];
    [self.labelTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.contentView.centerY);
        make.height.equalTo(self.contentView.height);
        make.left.mas_equalTo(@(20));
        make.width.mas_equalTo(@(90));
    }];
}

- (void)addTextField {
    [self.contentView addSubview:self.textField];
    [self.textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.contentView.centerY);
        make.height.equalTo(self.contentView.height).with.offset(-10);
        make.left.mas_equalTo(@(110));
        make.width.equalTo(self.contentView.width).with.offset(-130);
    }];
}

- (void)addButtonProvince {
    [self.contentView addSubview:self.buttonProvince];
    [self.buttonProvince mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.contentView.centerY);
        make.height.equalTo(self.contentView.height).with.offset(-10);
        make.left.mas_equalTo(@(120));
        make.width.mas_equalTo(@(80));
    }];
}

- (void)addButtonCity {
    [self.contentView addSubview:self.buttonCity];
    [self.buttonCity mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.contentView.centerY);
        make.height.equalTo(self.contentView.height).with.offset(-10);
        make.left.mas_equalTo(@(220));
        make.width.mas_equalTo(@(80));
    }];
}

- (void)addLabelInfo {
    [self.contentView addSubview:self.labelInfo];
    [self.labelInfo mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.contentView.centerY);
        make.height.equalTo(self.contentView.height);
        make.left.mas_equalTo(@(110));
        make.width.equalTo(self.contentView.width).with.offset(-150);
    }];
}

- (void)addTextView {
    [self.contentView addSubview:self.textView];
 
    [self.textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.contentView.centerY);
        make.height.equalTo(self.contentView.height).with.offset(-10);
        make.left.mas_equalTo(@(20));
        make.width.equalTo(self.contentView.width).with.offset(-40);
    }];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    
}

#pragma mark - UITextViewDelegate Methods

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView {
    if ([textView.text isEqualToString:RaiseCellTextViewInfo]) {
        textView.text = @"";
    }
    if (self.blockTextViewShouldBeginEdit) {
        self.blockTextViewShouldBeginEdit();
    }
    return YES;
}

- (void)textViewDidEndEditing:(UITextView *)textView {
    if (textView.text.length == 0) {
        textView.text = RaiseCellTextViewInfo;
    }
    if (self.blockTextViewDidEndEdit) {
        self.blockTextViewDidEndEdit();
    }
}

#pragma mark - button click

- (void)provinceButtonClick {
    if (self.blockProvinceButtonClick) {
        self.blockProvinceButtonClick();
    }
}
- (void)cityButtonClick {
    if (self.blockCityButtonClick) {
        self.blockCityButtonClick();
    }
}
#pragma mark - instance 

- (UILabel *)labelTitle {
    if (!_labelTitle) {
        _labelTitle = [[UILabel alloc] init];
        [_labelTitle setTextColor:[UIColor colorWithHexString:@"#666666"]];
    }
    return _labelTitle;
}

- (UITextField *)textField {
    if (!_textField) {
        _textField = [[UITextField alloc] init];
        _textField.font = [UIFont systemFontOfSize:15];
        [_textField setBackgroundColor:[UIColor colorWithHexString:@"f2f2f2"]];
    }
    return _textField;
}

- (UIButton *)buttonProvince {
    if (!_buttonProvince) {
        _buttonProvince = [UIButton buttonWithType:UIButtonTypeCustom];
        [_buttonProvince setTitle:@"省份" forState:UIControlStateNormal];
        [_buttonProvince setTitleColor:[UIColor colorWithHexString:@"666666"] forState:UIControlStateNormal];
        _buttonProvince.titleLabel.font = [UIFont systemFontOfSize:15];
        [_buttonProvince setImage:[UIImage imageNamed:@"raise_san"] forState:UIControlStateNormal];
        [_buttonProvince.imageView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(_buttonProvince.bottom).with.offset(0);
            make.right.equalTo(_buttonProvince.right).with.offset(0);
            make.width.mas_equalTo(@(10));
            make.height.mas_equalTo(@(10));
        }];
        [_buttonProvince addTarget:self action:@selector(provinceButtonClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _buttonProvince;
}

- (UIButton *)buttonCity {
    if (!_buttonCity) {
        _buttonCity = [UIButton buttonWithType:UIButtonTypeCustom];
        [_buttonCity setTitle:@"城市" forState:UIControlStateNormal];
        [_buttonCity setTitleColor:[UIColor colorWithHexString:@"666666"] forState:UIControlStateNormal];
        _buttonCity.titleLabel.font = [UIFont systemFontOfSize:15];
        [_buttonCity setImage:[UIImage imageNamed:@"raise_san"] forState:UIControlStateNormal];
        [_buttonCity.imageView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(_buttonCity.bottom).with.offset(0);
            make.right.equalTo(_buttonCity.right).with.offset(0);
            make.width.mas_equalTo(@(10));
            make.height.mas_equalTo(@(10));
        }];
        [_buttonCity addTarget:self action:@selector(cityButtonClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _buttonCity;
}

- (UILabel *)labelInfo {
    if (!_labelInfo) {
        _labelInfo = [[UILabel alloc] init];
        [_labelInfo setTextColor:[UIColor colorWithHexString:@"a3a3a3"]];
    }
    return _labelInfo;
}

- (UITextView *)textView {
    if (!_textView) {
        _textView = [[UITextView alloc] init];
        [_textView setBackgroundColor:[UIColor colorWithHexString:@"f2f2f2"]];
        [_textView setText:RaiseCellTextViewInfo];
        _textView.delegate = self;
        [_textView setTextColor:[UIColor colorWithHexString:@"a3a3a3"]];
        [_textView setFont:[UIFont systemFontOfSize:16]];
    }
    return _textView;
}


@end
